using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class FoodItem : BaseItem
	{
		private Color32 bodyColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);

		private SpriteRenderer modelSprite;

		private float burnTime;

		private bool isChangeWhenFall;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			modelSprite = model.GetComponent<SpriteRenderer>();
			if (data.bodyColor.r != 0 && data.bodyColor.g != 0 && data.bodyColor.b != 0)
			{
				SetColor(data.bodyColor);
			}
		}

		protected override bool ScriptCheckCollsEnd()
		{
			if (base.itemData.type == 8)
			{
				for (int i = 0; i < itemColls.Count; i++)
				{
					BaseItem item = itemColls[i].item;
					if (ContainsChild(item) || item.itemData.scriptType != 30 || item.itemData.type != 8 || !((FoodItem)item).CanStackUp())
					{
						continue;
					}
					BaseItem baseItem = this;
					BaseItem baseItem2 = item;
					if (!string.IsNullOrEmpty(base.itemData.changeId) && childItems.Count <= 0)
					{
						BaseItem baseItem3 = Singleton<Scene>.Instance.ChangeItem(this, base.itemData.changeId);
						if (baseItem3 != null)
						{
							baseItem = baseItem3;
						}
					}
					if (!string.IsNullOrEmpty(item.itemData.changeId) && item.childItems.Count <= 0)
					{
						bool flag = false;
						BaseItem baseItem4 = null;
						DepthType depthType = DepthType.Ahead;
						AttachType attachType = AttachType.None;
						if (item.parentItem != null && item.parentItem.itemData.scriptType == 1 && (item.attachType == AttachType.LeftHandTake || item.attachType == AttachType.RightHandTake))
						{
							flag = true;
							baseItem4 = item.parentItem;
							depthType = item.depthSystem.depthType;
							attachType = item.attachType;
						}
						BaseItem baseItem5 = Singleton<Scene>.Instance.ChangeItem(item, item.itemData.changeId);
						if (baseItem5 != null)
						{
							baseItem2 = baseItem5;
						}
						if (flag)
						{
							baseItem4.AttachItem(baseItem2, depthType, attachType, true);
						}
						else
						{
							baseItem2.DropDown(item.transform.position, true);
						}
					}
					baseItem2.AttachItem(baseItem, DepthType.Front, AttachType.StackUp, true);
					baseItem2.depthSystem.SortBaseNode();
					Singleton<AudioManager>.Instance.PlaySound("sound_explode");
					if (baseItem != null)
					{
						Vector3 pos = new Vector3(baseItem.transform.position.x, baseItem.transform.position.y + 0.2f, baseItem2.transform.position.z - 0.001f - 0.0004f);
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", pos, 1f);
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", pos, 3f);
					}
					return true;
				}
			}
			return false;
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			if (base.itemData.scriptParams.Equals("1") && moveAnimType == MoveAnimType.FreeFall && !string.IsNullOrEmpty(base.itemData.changeId) && childItems.Count <= 0 && base.transform.position.y - moveEndPoint.y > 1.4f)
			{
				needRebound = false;
				isChangeWhenFall = true;
			}
		}

		protected override void FreeFallComplete()
		{
			base.FreeFallComplete();
			if (isChangeWhenFall)
			{
				isChangeWhenFall = false;
				BaseItem baseItem = Singleton<Scene>.Instance.ChangeItem(this, base.itemData.changeId);
				baseItem.DropDown(baseItem.transform.position, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_explode");
				Vector3 pos = new Vector3(baseItem.transform.position.x, baseItem.transform.position.y + 0.2f, baseItem.transform.position.z - 0.001f - 0.0004f);
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", pos, 1f);
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", pos, 3f);
			}
		}

		public bool CanStackUp()
		{
			if (childItems.Count > 0)
			{
				return false;
			}
			if (!InGroundOrHand())
			{
				return false;
			}
			return true;
		}

		public bool InGroundOrHand()
		{
			if (parentItem == null)
			{
				return true;
			}
			if (parentItem.itemData.scriptType == 30)
			{
				return ((FoodItem)parentItem).InGroundOrHand();
			}
			if (parentItem.itemData.scriptType == 1)
			{
				return true;
			}
			return false;
		}

		private void SetEfcColor(Transform efc, Color32 colorVal)
		{
			if (!(efc == null))
			{
				ParticleSystem[] componentsInChildren = efc.GetComponentsInChildren<ParticleSystem>();
				ParticleSystem.MinMaxGradient startColor = default(ParticleSystem.MinMaxGradient);
				startColor.color = colorVal;
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					ParticleSystem.MainModule main = componentsInChildren[i].main;
					main.startColor = startColor;
				}
			}
		}

		public override void StartBurn()
		{
			base.StartBurn();
			burnTime = 0f;
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.canBurn)
				{
					childItems[i].StartBurn();
				}
			}
		}

		public override void Burn(float time)
		{
			burnTime += time;
			if (burnTime > 0.6f)
			{
				burnTime = 0f;
				if (bodyColor.g > 190)
				{
					bodyColor.g -= 6;
					bodyColor.b -= 9;
					if (bodyColor.g <= 190)
					{
						bodyColor.r = byte.MaxValue;
						bodyColor.g = 190;
						bodyColor.b = 160;
					}
					SetColor(bodyColor);
				}
			}
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.canBurn)
				{
					childItems[i].Burn(time);
				}
			}
		}

		private void SetColor(Color32 color)
		{
			bodyColor = color;
			modelSprite.color = color;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (bodyColor.r != byte.MaxValue || bodyColor.g != byte.MaxValue || bodyColor.b != byte.MaxValue)
			{
				obj["27"] = bodyColor.r;
				obj["28"] = bodyColor.g;
				obj["29"] = bodyColor.b;
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (item.parentItem != null)
			{
				return false;
			}
			if (attachType == AttachType.StackUp)
			{
				item.SetParent(GetStackPoint());
				if (updatePosition)
				{
					item.SetLocalPosition(0f, 0f);
				}
			}
			return base.AttachItem(item, depthType, attachType, updatePosition);
		}
	}
}
